<template>
  <div>
    <vab-card>
      <span class="leve_title">发票管理</span>
    </vab-card>
    <vab-card shadow="always">
      <template #header>
        <div class="flex_sb">
          <span class="leve2_title">订单列表</span>
          <div class="flex_just_end">
            <VabAuthButton
              type="primary"
              title="申请开票"
              @click="handleClick('applyFor')"
            />
          </div>
        </div>
      </template>
      <el-table
        border
        height="580"
        style="width: 100%"
        :data="data.dataSource"
        v-loading="data.loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          :selectable="selectable"
          align="center"
          width="40"
        />
        <el-table-column label="序号" align="center" width="60">
          <template #default="scope">
            <span
              class="ellipsis"
              v-text="(data.pageNum - 1) * data.pageSize + (scope.$index + 1)"
            ></span>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, i) in data.cloums"
          :key="i"
          :width="item.width"
          :prop="item.prop"
          :label="item.label"
          :align="
            item.type == 'money' || item.type == 'number' ? 'right' : 'center'
          "
        >
          <template #header>
            <span class="text_center">{{ item.label }}</span>
          </template>
          <template #default="{ row }">
            <!-- 自定义 -->
            <div v-if="item.type == 'custom'">
              <span v-if="item.prop == 'status_str'">
                <el-tag :type="statusColor(row.status_str)">
                  {{ row.status_str || '--' }}
                </el-tag>
              </span>
              <span class="ellipsis" v-if="item.prop == 'shipping_address'">
                {{ row.shipping_address || '' }} {{ row.address || '' }}
              </span>
              <span
                v-if="item.prop == 'c_order_uid'"
                class="ellipsis link"
                @click="linkDetail(row)"
              >
                {{ row.c_order_uid || '--' }}
              </span>

              <span v-if="item.prop == 'invoice_name'">
                <el-tag>
                  {{ row.invoice_name || '--' }}
                </el-tag>
              </span>
            </div>
            <!-- 正常 -->
            <div v-else class="ellipsis">
              {{ _FieldFormat(row[item.prop], item.type) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" fixed="right">
          <template #default="{ row }">
            <VabButton
              title="下载"
              type="primary"
              v-if="row.status_str == '已开票'"
              @click="handleClick('download', row)"
            />
            <!-- <VabButton
              v-else
              title="查看详情"
              type="primary"
              @click="linkDetail(row)"
            /> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="flex_just_end">
        <el-pagination
          background
          small="small"
          v-model:current-page="data.pageNum"
          v-model:page-size="data.pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :page-sizes="[20, 50, 100, 200]"
          :pager-count="10"
          :total="data.count"
          @current-change="handlePageNum"
          @size-change="handlePageSize"
        />
      </div>
    </vab-card>
    <!-- 开票信息确认 -->
    <InfoConfirmDialog ref="infoConfirmDialogRef" @refresh="_GetProductList" />
  </div>
</template>
<!-- 库存查询 -->
<script lang="ts" setup>
  defineOptions({
    name: 'InvoiceIndex',
  })
  /**
   * @import
   */
  import { getInvoiceList } from '~/src/api/dms/invoice'
  import {
    DateFormat,
    FieldFormat,
    DownloadFile,
    CheckUserAuth,
  } from '~/src/utils/utils'
  import { InvoicableOrdersCloums } from './components/tools'
  import InfoConfirmDialog from './components/InfoConfirmDialog.vue'
  import dayjs from 'dayjs'
  /**
   * @var
   */
  const proxy: any = getCurrentInstance()?.proxy
  const router = useRouter()
  const route: any = useRoute()

  const infoConfirmDialogRef = ref()

  const data = reactive<any>({
    dataSource: [],
    cloums: [],
    pageNum: 1,
    pageSize: 20,
    count: 0,
  })
  data.dataSource = []
  data.cloums = InvoicableOrdersCloums

  const currentRow = ref<any>(null) //选中块

  /**
   * @method
   */
  // 多选
  const handleSelectionChange = (row: any) => {
    currentRow.value = row
  }
  // 页码
  const handlePageNum = (value: any) => {
    data.pageNum = value
    _GetProductList()
  }
  // 页数
  const handlePageSize = (value: any) => {
    data.pageSize = value
    _GetProductList()
  }

  // 点击事件
  const handleClick = (type: any, row: any = {}) => {
    // 下载
    if (type == 'download') {
      const times = dayjs().valueOf()
      DownloadFile(row.img_path, `发票_${times}`)
      return
    }

    if (!currentRow.value) {
      proxy.$Message('请选择需要操作的订单！', 'warning')
      return
    }
    // 申请开票
    if (type == 'applyFor') {
      infoConfirmDialogRef.value.open(currentRow.value)
    }
  }

  // 跳转详情
  const linkDetail = ({ c_order_uid }: any) => {
    router.push('/procure/orderTrackDateil?order_no=' + c_order_uid)
  }

  // 多选 禁用
  const selectable = ({ status_str, sales_org_id }: any, i: number) => {
    // 禁用项
    if (
      status_str == '开票中' ||
      status_str == '已开票' ||
      status_str == '已过开票周期'
    )
      return false

    //结算组织 不同不可选择
    if (currentRow.value) {
      const row = currentRow.value[0]
      if (row.sales_org_id == sales_org_id) {
        return true
      } else {
        return false
      }
    }

    return true
  }

  // 字段级格式化
  const _FieldFormat = (value: string, type = '') => {
    if (type == 'time') return DateFormat(value, 'YYYY-MM-DD')
    return FieldFormat(value, type)
  }

  // 状态tag_color
  const statusColor = (value: string) => {
    if (value.indexOf('待') == 0) return 'warning'
    if (value.indexOf('中') == 0) return 'warning'
    if (value.indexOf('已') == 0) return 'success'
    if (value.indexOf('失败') == 0) return 'danger'
    return 'info'
  }

  const resetPage = () => {
    data.pageSize = 20
    data.pageNum = 1
  }

  // 获取数据
  const _GetProductList = async () => {
    data.loading = true

    let query = {
      page: data.pageNum,
      limit: data.pageSize,
      filter: {},
    }

    try {
      let res = await getInvoiceList({ ...query })
      data.dataSource = res.data.list
      data.count = res.data.count
      data.loading = false
    } catch (error) {}
  }
  onMounted(() => {
    _GetProductList()
  })
</script>

<style lang="scss" scoped>
  @import '../../components/public.scss';
</style>
